/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 作者: 090922(陈伟)
 * <p>
 * 文件名: com.oceansoft.mobile.econsole.business.sys.controller.SysDictController.java 
 * <p>
 * 类产生时间: 2014/5/26 0026 下午 19:24
 * <p>
 * 负责人: 090922(陈伟)
 * <p>
 * Email:javacspring@gmail.com
 * <p>
 * 所在组 : 掌上公安应用平台
 * <p>
 * 所在部门: 开发部--手持技术部
 * <p>
 * <p>
 */
package cn.com.oceansoft.business.sys.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.sys.entity.DictItem;
import cn.com.oceansoft.business.sys.service.ISysDictService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static org.springframework.web.bind.annotation.RequestMethod.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: Administrator
 * @time: 2014/5/26 0026 下午 19:24
 */
@Controller
@RequestMapping("sys/dict")
public class SysDictController extends BaseController {

    @Resource
    private ISysDictService sysDictService;

    /**
     * 进入系统字典管理首页
     *
     * @param model 数据Model
     * @return String
     */
    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String gotoDictIndex(Model model) {
        model.addAttribute("node", sysDictService.findAllDictByZtree());
        log("查看字典管理列表");
        return "sys/sys-dict-list";
    }

    /**
     * 进入列表信息
     *
     * @param parentId 父id
     * @param query    分页器
     * @param model
     * @return 地址
     */
    @RequestMapping(value = "sub/{parent_id}", method = GET)
    public String gotoDictSubList(@PathVariable("parent_id") int parentId, Query query, Model model) {
        query.setOrderField("pid,sort");
        query.setOrderDirection("asc");
        model.addAttribute("query", query);
        model.addAttribute("pager", sysDictService.findPageByCondition(query, parentId));
        model.addAttribute("pid", parentId);
        return "sys/sys-dict-sub-list";
    }

    /**
     * 列表分页显示
     *
     * @param model
     * @param query 分页查询条件
     * @return 页面地址
     */
    @RequestMapping(value = "sub/list", method = POST)
    public String loadPageByCondition(Query query, @RequestParam("pid") int parentId, Model model) {
        model.addAttribute("pager", sysDictService.findPageByCondition(query, parentId));
        model.addAttribute("pid", parentId);
        return "sys/sys-dict-sub-list";
    }

    /**
     * 新增字典页面
     *
     * @return String
     */
    @RequestMapping(value = "new/{sel_id}", method = GET)
    public String openAddDict(Model model, @PathVariable("sel_id") int pid) {
        model.addAttribute("dict", sysDictService.findById(pid));
        return "sys/sys-dict-new";
    }

    /**
     * 打开编辑字典页面或者展示
     *
     * @return String
     */
    @RequestMapping(value = "view/{dict_id}", method = GET)
    public String viewDict(@PathVariable("dict_id") int dict_id, Model model) {
        model.addAttribute("dictItem", sysDictService.findById(dict_id));
        return "sys/sys-dict-edit";
    }

    /**
     * 字典新增
     *
     * @param dict 新增字典实体对象
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "new", method = POST)
    public Result addDict(DictItem dict) {
        log(String.format("新增字典值%s", dict.getVal()));
        if(dict.getExt()==null){
            dict.setExt("");
        }
        Result result = sysDictService.save(dict);
        //如果更新成功 更新上下文中的数据
        if (result.isSucc()) {
            updateDictItem(dict);
        }
        return result;
    }

    /**
     * 删除字典信息
     *
     * @param dictid 字典id
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "del/{dictid}", method = DELETE)
    public Result deleteDict(@PathVariable("dictid") int dictid) {
        Result rt;
        DictItem dict = sysDictService.findById(dictid);
        //判断系统字典  系统字典无法删除
        if (dict != null && dict.getParentId() == 0) {
            rt = new Result(Result.FAILURE, "系统字典项！");
        } else {
            rt = sysDictService.deleteDictItemById(dictid);
            log(String.format("删除编号为%d字典值%s", dictid, rt.isSucc() ? "成功" : "失败"));
        }
        return rt;
    }

    /**
     * 字典编辑
     *
     * @param dict 新角色实体对象
     * @return Result
     */
    @ResponseBody
    @RequestMapping(value = "edit", method = POST)
    public Result editDict(DictItem dict) {
        if(dict.getExt()==null){
            dict.setExt("");
        }
        Result result = sysDictService.update(dict);
        //如果编辑成功 更新上下文中的数据
        if (result.isSucc()) {
            updateDictItem(dict);
        }
        return result;
    }
}
